
package com.yixun.qfbao.xf.model.enums;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;

import com.yixun.common.utils.EnumUtil;
import com.yixun.qfbao.constant.StringConstant;
import com.yixun.qfbao.util.StringUtil;

public enum ReportWayEnum implements Serializable {

    UNLIMITED("0", "全号/前三后四", null, null, null),
    FULL_NUMBER("1", "全号", null, null, null),
    BEFORE_THREE_AFTER_FOUR("2", "前三后四", "(\\d{3})\\d{4}(\\d{4})","(\\d{3})(\\*{4})(\\d{4})", "$1****$2"),
    BEFORE_FOUR_AFTER_THREE("3", "前四后三", "(\\d{4})\\d{4}(\\d{3})","(\\d{4})(\\*{4})(\\d{3})", "$1****$2"),
    BEFORE_THREE_AFTER_FIVE("4", "前三后五", "(\\d{3})\\d{3}(\\d{5})","(\\d{3})(\\*{3})(\\d{5})", "$1***$2"),
    BEFORE_TWO_AFTER_SIX("5", "前二后六", "(\\d{2})\\d{3}(\\d{6})","(\\d{2})(\\*{3})(\\d{6})", "$1***$2"),
    BEFORE_FOUR_AFTER_FOUR("6", "前四后四", "(\\d{4})\\d{3}(\\d{4})","(\\d{4})(\\*{3})(\\d{4})", "$1***$2"),
    ;


    private final String no;

    private final String des;

    private final String regex;

    private final String hideRegex;
    
    private final String site;

    static {
        EnumUtil.ENUMS_TO_VIEW.put("report_way", ReportWayEnum.values());
    }

    ReportWayEnum(String no, String des, String regex, String hideRegex, String sitex) {
        this.no = no;
        this.des = des;
        this.regex = regex;
        this.hideRegex = hideRegex;
        this.site = sitex;
    }

    /**
     * @return Returns the no.
     */
    public String getNo() {
        return no;
    }

    public String getDes() {
        return des;
    }

    public String getRegex() {
		return regex;
	}

    public String getHideRegex() {
        return hideRegex;
    }

	public String getSite() {
		return site;
	}

	/**
     * 通过枚举<code>des</code>获得枚举
     *
     * @param des
     * @return
     */
    public static ReportWayEnum getByDescription(String des) {
        if (StringUtils.isBlank(des)) {
            return null;
        }
        for (ReportWayEnum menum : values()) {
            if (menum.getDes().equals(des)) {
                return menum;
            }
        }
        return null;
    }

    public static ReportWayEnum getByNo(String no) {
        if (StringUtils.isBlank(no)) {
            return null;
        }
        for (ReportWayEnum menum : values()) {
            if (menum.getNo().equals(no)) {
                return menum;
            }
        }
        return null;
    }

    public static String getDescByNo(String no) {

        ReportWayEnum byNo = getByNo(no);

        return byNo == null ? null : byNo.getDes();

    }
    
    /**
     * 	根据报备方式转换手机号, 
     * 	对应的枚举若regex或者site为null会直接返回传递过来的手机号
     * @param reportWay
     * @return
     */
    public static String convertMobileByReportWay(String mobile, String reportWay) {
    	if(StringUtils.isBlank(mobile) || StringUtils.isBlank(reportWay) || mobile.contains(StringConstant.THREE_STAR)){
			return mobile;
		}
    	ReportWayEnum reportWayEnum = getByNo(reportWay);
    	
    	if(reportWayEnum != null && StringUtil.isNotNull(reportWayEnum.getRegex(), reportWayEnum.getSite())) {
    		mobile = mobile.replaceAll(reportWayEnum.getRegex(), reportWayEnum.getSite());
    	}
    	return mobile;
    }
    
    /**
     * 	检查号码和隐号方式是否匹配
     * @param mobile
     * @param reportWay
     * @return
     */
    public static boolean checkMobile(String mobile, String reportWay) {
    	if(StringUtils.isBlank(mobile) || StringUtils.isBlank(reportWay)){
			return false;
		}

    	if(ReportWayEnum.UNLIMITED.getNo().equals(reportWay)){
    	    // 判断如果是隐号,则必须是前三后四.
            if(mobile.contains(StringConstant.THREE_STAR)){
                boolean macthFlag = mobile.matches(ReportWayEnum.BEFORE_THREE_AFTER_FOUR.getHideRegex());
                if(macthFlag){
                    return true;
                }else {
                    return false;
                }
            }else{
                return true;
            }
        }
        ReportWayEnum reportWayEnum = getByNo(reportWay);
    	return mobile.matches(reportWayEnum.getHideRegex());
    }

}
